<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class M_course extends CI_Model {
    /**
     * 继承父类
     */
    function __construct(){
        parent::__construct();
    }
    /**
     * 添加课程
     * @param  [type] $str [description]
     * @return [type]      [description]
     */
    public function course_add($str){
        $keys='';
        $into='';
        foreach ($str as $key => $value) {
            $keys.=$keys?(" , ".$key):$key;
            $into.=$into?(" , '".$value."'"):("'".$value."'");
        }
        $query="INSERT INTO stu_course (".$keys.") VALUES(".$into.")";
        return $this->db->query($query);
    }
    /**
     * 课程列表
     * @return [type] [description]
     */
    public function course_list(){
        $query="SELECT * FROM stu_course ORDER BY id DESC";
        $result=$this->db->query($query);
        return $result->result();
    }
    /**
     * 部分课程列表
     * @param  [type] $str [description]
     * @return [type]      [description]
     */
    public function course_some_list($str){
        $where='';
        foreach ($str as $key => $value) {
            if($value){
                $where.=$where?(" AND ".$key."='".$value."'"):($key."='".$value."'");
            }
        }
        $query="SELECT * FROM stu_course WHERE ".$where;
        $result=$this->db->query($query);
        return $result->result();
    }
    /**
     * 搜索功能，课程搜索
     * @param  [type] $name 课程名
     * @return [type]       [description]
     */
    public function course_search($name){
        $name="%".$name."%";
        $query="SELECT * FROM stu_course WHERE name LIKE '".$name."'";
        $result=$this->db->query($query);
        return $result->result();
    }
    /**
     * 课程基本信息
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function course_base_info($id){
        $query="SELECT * FROM stu_course WHERE id='".$id."'";
        $result=$this->db->query($query);
        return  $result->result();
    }
    /**
     * 跟新课程基本信息
     * @param  [type] $str [description]
     * @return [type]      [description]
     */
    public function course_base_update($str){
        $set='';
        $where='';
        foreach ($str as $key => $value) {
            if($value && $key!='id'){
                $set.=$set?(" , ".$key."='".$value."'"):($key."='".$value."'");
            }
            if($key=='id'){
                $where.=$where?(" AND ".$key."='".$value."'"):($key."='".$value."'");
            }
        }
        $query="UPDATE stu_course SET ".$set." WHERE ".$where;
        return $this->db->query($query);
    }
    /**
     * 删除课程基本信息
     * @param  [type] $str [description]
     * @return [type]      [description]
     */
    public function course_base_delete($str){
        $where='';
        foreach ($str as $key => $value) {
            if($value){
                $where.=$where?(" AND ".$key."='".$value."'"):($key."='".$value."'");
            }
        }
        $query="DELETE FROM stu_course WHERE ".$where;
        return $this->db->query($query);
    }
    /**
     * 添加课程安排
     * @return [type] [description]
     */
    public function course_plan_add($str,$style){
        $keys='';
        $into='';
        foreach ($str as $key => $value) {
            if($value){
                $keys.=$keys?(" , ".$key):$key;
                $into.=$into?(" , '".$value."'"):("'".$value."'");
            }
        }
        if($style=='1'){
            $query="INSERT INTO stu_course_plan (".$keys.") VALUES(".$into.")";
        }else{
            $query="INSERT INTO stu_elective_course_plan (".$keys.") VALUES(".$into.")";
        }
        return $this->db->query($query);
    }
    /**
     * 删除课程安排
     * @param  [type] $str [description]
     * @return [type]      [description]
     */
    public function course_plan_delete($str){
        $where='';
        foreach ($str as $key => $value) {
            if($value){
                $where.=$where?(" AND ".$key."='".$value."'"):($key."='".$value."'");
            }
        }
        $query="DELETE FROM stu_course_plan WHERE ".$where;
        return $this->db->query($query);
    }
    /**
     * 查询选修课程安排
     * @param  [type] $str [description]
     * @return [type]      [description]
     */
    public function elective_course_plan_search($str){
        $where='';
        foreach ($str as $key => $value) {
            if($value){
                $where.=$where?(" AND ".$key."='".$value."'"):($key."='".$value."'");
            }
        }
        $query="SELECT * FROM stu_elective_course_plan WHERE ".$where;
        $result=$this->db->query($query);
        return $result->result();
    }
    /**
     * 课程全部信息，返回id 为课程安排id
     * @param  [type] $params [description]
     * @return [type]         [description]
     */
    public function search_course_info($str,$style='2'){
        $where='';
        foreach ($str as $key => $value) {
            if($value){
                $where.=$where?(" AND ".$key."='".$value."'"):($key."='".$value."'");
            }
        }
        if($style=='1'){
            $query="SELECT * FROM stu_course INNER JOIN stu_course_plan ON stu_course_plan.c_id=stu_course.id WHERE ".$where;
        }else{
            $query="SELECT * FROM stu_course INNER JOIN stu_elective_course_plan ON stu_elective_course_plan.c_id=stu_course.id WHERE ".$where;
        }
        $result=$this->db->query($query);
        return $result->result();
    }
    /**
     * 查询课程安排
     * @param  [type] $str [description]
     * @return [type]      [description]
     */
    public function course_plan_search($str,$style='1'){
        $where='';
        foreach ($str as $key => $value) {
            if($value){
                $where.=$where?(" AND ".$key."='".$value."'"):($key."='".$value."'");
            }
        }
        if($style=='1'){
            $query="SELECT * FROM stu_course_plan WHERE ".$where;
        }else{
            $query="SELECT * FROM stu_elective_course_plan WHERE ".$where;
        }
        $result=$this->db->query($query);
        return $result->result();
    }
    /**
     * 通过课程查找教师信息集
     * @return [type] [description]
     */
    public function course_teacher_info($str){
        $where='';
        foreach ($str as $key => $value) {
            if($value){
                $where.=$where?(" AND ".$key."='".$value."'"):($key."='".$value."'");
            }
        }
        $query="SELECT * FROM stu_course INNER JOIN stu_teacher ON stu_course.id=stu_teacher.c_id WHERE ".$where;
        $result=$this->db->query($query);
        return $result->result();
    }
    /**
     * 跟新课程安排
     * @param  [type] $str       [description]
     * @param  [type] $condition [description]
     * @return [type]            [description]
     */
    public function course_plan_update($str,$condition,$style='2'){
        $where='';
        foreach ($condition as $key => $value) {
            if($value){
                $where.=$where?(" AND ".$key."='".$value."'"):($key."='".$value."'");
            }
        }
        $set='';
        foreach ($str as $key => $value) {
            if($value){
                $set.=$set?(" , ".$key."='".$value."'"):($key."='".$value."'");
            }
        }
        if($style=='1'){
            $query="UPDATE stu_course_plan SET ".$set." WHERE ".$where;
        }else{
            $query="UPDATE stu_elective_course_plan SET ".$set." WHERE ".$where;
        }
        return $this->db->query($query);
    }
    /**
     * 选修,选课人数增删
     * @param  [type] $str [description]
     * @return [type]      [description]
     */
    public function elective_count_update($param='1',$s_id,$condition){
        $where='';
        foreach ($condition as $key => $value) {
            if($value){
                $where.=$where?(" AND ".$key."='".$value."'"):($key."='".$value."'");
            }
        }
        if($param=='1'){
            $query="UPDATE stu_elective_course_plan SET now_count=now_count+1,s_id='".$s_id."' WHERE ".$where;
        }else{
            $query="UPDATE stu_elective_course_plan SET now_count=now_count-1,s_id='".$s_id."' WHERE ".$where;
        }
        return $this->db->query($query);
    }
}